#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "nextarea.h"
#include"gamearea.h"
#include <QKeyEvent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    Playlist =new QMediaPlaylist;     //创建新的播放清单
    player=new QMediaPlayer;          //创建新的播放器
    //audioOutput = new QAudioOutput(this);
    //audioOutput->setVolume(0.5);
    QString filePath = "/home/ytx/tetris/background1.mp3";
    player->setMedia(QUrl::fromLocalFile(filePath));
    //Playlist->addMedia(QUrl::fromLocalFile("/home/ytx/tetris/background1.mp3")); //往播放清单中添加音乐文件
    // // Playlist->addMedia(QUrl::fromLocalFile("D:/MyQT/Teris/media/2.mp3"));
    // // Playlist->addMedia(QUrl::fromLocalFile("D:/MyQT/Teris/media/3.mp3"));
    // Playlist->setCurrentIndex(0);  //设置默认播放开始位置
    // Playlist->setPlaybackMode(QMediaPlaylist::Loop); //设置循环播放
    // player->setPlaylist(Playlist); //播放清单导入播放器
    player->play(); //播放器开播
    setFixedSize(1000,800); //设置窗体为固定大小
    setWindowTitle(tr("俄罗斯方块"));//设置窗体标题
    connect(ui->widgetGameArea,&GameArea::sigUpdateScore,this,&MainWindow::slotUpdateScore);
    connect(ui->widgetGameArea,&GameArea::sigUpdateLevel,this,&MainWindow::slotUpdateLevel);
    connect(ui->widgetGameArea,&GameArea::sigUdateTimeEslape,this,&MainWindow::slotUpdateTime);
    //GameArea通过信号sigUpdateNextItem 通知 NextArea 刷新下一个元素
    connect(ui->widgetGameArea,&GameArea::sigUpdateNextItem,ui->nextarea,&NextArea::slotUpdateNextItem);
    ui->pbPause->setEnabled(true);
    ui->pbStart->setEnabled(true);
    ui->checkBox->setChecked(true);
    setFocusPolicy(Qt::StrongFocus);
    Gamestatus=true;


}
void MainWindow::slotUpdateScore(int nScore)
{
    ui->labelScore->setText(QString::number(nScore));
}

void MainWindow::slotUpdateLevel(int nSpeed)
{
    ui->labelSpeed->setText(QString::number(nSpeed));
}

void MainWindow::slotUpdateTime(int tplayed)
{
    int sec=tplayed/1000;

    ui->labelTime->setText(QString::number(sec)+"s");
}
void MainWindow::on_checkBox_stateChanged(int arg1)
{

    if(arg1)
        player->play();
    else
        player->stop();

}

void MainWindow::on_pbStart_clicked()
{
    Gamestatus=true;
    ui->widgetGameArea->NewGame();
    ui->pbStart->setEnabled(false);
    ui->pbPause->setEnabled(true);
    ui->pbStart->setEnabled(true);
}


void MainWindow::on_pbPause_clicked()
{
    ui->widgetGameArea->PauseGame(Gamestatus);
    bool ok;
    if(Gamestatus)
    {
        ui->pbPause->setText(tr("重新开始"));
        Gamestatus=false;
        mTime=(ui->label_9->text()).toInt(&ok,10);
    }
    else
    {
        ui->pbPause->setText(tr("暂停"));

        Gamestatus=true;
    }
}

void MainWindow::on_pbStop_clicked()
{
    ui->widgetGameArea->StopGame();
    ui->pbStart->setEnabled(false);
    ui->pbStart->setEnabled(true);
    ui->pbPause->setEnabled(false);
}

void MainWindow::keyPressEvent(QKeyEvent *e)
{

    ui->widgetGameArea->KeyPressed(e->key());
    QMainWindow::keyPressEvent(e);
}
MainWindow::~MainWindow()
{
    delete ui;
}
